<?php

/**
 *
 **/

class simplepiePlugin extends Plugin
{
	private $old_error_reporting = null;
	
	public function action_update_check()
	{
	 	Update::add( 'simplepiePlugin', '21CB3E48-5674-11DE-A840-A19556D89593', $this->info->version );
	}
	
	/**
	 * Makes sure everything is setup proper.
	 **/
	public function action_plugin_activation( $file )
	{
        if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
			if ( Options::get( 'simplepie__count' ) == null ) {
				Options::set( 'simplepie__count', '5' );
			}
			if ( Options::get( 'simplepie__url' ) == null ) {
				Options::set( 'simplepie__url', 'http://rss.news.yahoo.com/rss/topstories' );
			}
		}
	}

	/**
	* Executes when the admin plugins page wants to display the UI for a particular plugin action.
	* Displays the plugin's UI.
	*
	* @param string $plugin_id The unique id of a plugin
	* @param string $action The action to display
	**/
	public function action_plugin_ui( $plugin_id, $action )
	{
		// Display the UI for this plugin?
		if ( $plugin_id == $this->plugin_id() ) {
			// Depending on the action specified, do different things
			switch ( $action ) {
				case _t( 'Configure' ):
					$ui = new FormUI( get_class( $this ) );
					$ui->append( 'text', 'simplepie_url', 'option:' . 'simplepie__url', _t( 'Feed:' ) );
					$ui->append( 'text', 'simplepie_count', 'option:' . 'simplepie__count', _t( 'Number of feed results to show:' ) );
					$ui->append( 'submit', 'save', _t( 'Save' ) );
					$ui->set_option( 'success_message', _t( 'Configuration saved' ) );

					$ui->on_success( array( $this, 'updated_config' ) );
					$ui->out();
					break;
			}
		}
	}
	
	public function updated_config( $ui )
	{
		$ui->save();
		return false;
	}

	/**
	 * Plugin init action, executed when plugins are initialized.
	 */
	public function action_init()
	{
		$this->add_template( 'simplepie', dirname(__FILE__) . '/simplepie.php' );
		//Stack::add( 'template_stylesheet', array( $this->get_url() . '/tumble.css', 'screen' ), 'tumbleArchive' );
	}

	/**
	 * Add actions to the plugin page for this plugin
	 * @param array $actions An array of actions that apply to this plugin
	 * @param string $plugin_id The string id of a plugin, generated by the system
	 * @return array The array of actions to attach to the specified $plugin_id
	 **/
	public function filter_plugin_config( $actions, $plugin_id )
	{
		if ( $plugin_id == $this->plugin_id() ) {
			$actions[] = 'Configure';
		}

		return $actions;
	}
	

	public function supress_errors()
	{
		$this->old_error_reporting = error_reporting();
	    if( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
	        error_reporting( $this->old_error_reporting ^ E_STRICT ^ E_DEPRECATED );
	    }
        else {
	        error_reporting( $this->old_error_reporting ^ E_STRICT );
	    }
	}
	
	public function repress_errors()
	{
        error_reporting( $this->old_error_reporting );
	}
		
	public function theme_simplepie( $theme ) 
	{	        
	    // Remove E_STRICT, and E_DEPRECATED if needed (PHP 5.3+) for external PHP4 code
        $this->supress_errors();
   	    require_once dirname( __FILE__ ) . '/simplepie.inc';
   	    require_once dirname( __FILE__ ) . '/idn/idna_convert.class.php';        
        
        $feed = new SimplePie();
        $feed->set_feed_url( array( Options::get( 'simplepie__url' ) ) );
        $feed->enable_cache( false );
        
        
        // Make sure the page is being served with the UTF-8 headers.
        $feed->handle_content_type();
        
        $feed->init();
        
        $temp = null;
        $simple_items = null;
        $theme->simple_items = array();
        
        foreach( $feed->get_items( 0, Options::get( 'simplepie__count' ) ) as $item ) :
            $temp = array( 'permalink' => $item->get_permalink(),
                            'title' => $item->get_title(),
                            'description' => $item->get_description(),
                            'date' => $item->get_date('j F Y | g:i a') 
                             );
            $simple_items[] = $temp;
            $temp = null;
        endforeach;
   	    $this->repress_errors();
        
        $theme->simple_items = $simple_items;
	    return $theme->fetch( 'simplepie' );
    }
}
?>
